/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import model.Skill;
import model.user.Director;
import model.user.Governor;
import model.user.Staff;
import model.user.User;
import util.Utilization;
import view.user.ACIListModel;
import view.user.AdminView;

/**
 *
 * @author CUNEYT
 */
public class AdminController {

    private class AddUserListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                if (view.getUserNameTextField().getText().isEmpty() || view.getUserSurnameTextField().getText().isEmpty()
                        || view.getUserNAmeTextField().getText().isEmpty() || view.getUserPasswordTextField().getText().isEmpty()) {
                    throw new Exception("All of the fields must be filled!");
                } else {
                    if (view.getUserRolesComboBox().getSelectedItem().toString().equals(User.STAFF)) {
                        Staff st = new Staff(view.getUserNameTextField().getText(), view.getUserSurnameTextField().getText(),
                                view.getUserNAmeTextField().getText(), view.getUserPasswordTextField().getText().toCharArray());
                        Utilization.addUser(st);
                        users.add(st);
                    } else if (view.getUserRolesComboBox().getSelectedItem().toString().equals(User.DIRECTOR)) {
                        Director st = new Director(view.getUserNameTextField().getText(), view.getUserSurnameTextField().getText(),
                                view.getUserNAmeTextField().getText(), view.getUserPasswordTextField().getText().toCharArray());
                        Utilization.addUser(st);
                        users.add(st);
                    } else if (view.getUserRolesComboBox().getSelectedItem().toString().equals(User.GOVERNOR)) {
                        Governor st = new Governor(view.getUserNameTextField().getText(), view.getUserSurnameTextField().getText(),
                                view.getUserNAmeTextField().getText(), view.getUserPasswordTextField().getText().toCharArray());
                        Utilization.addUser(st);
                        users.add(st);
                    } else {
                        User st = new User(view.getUserNameTextField().getText(), view.getUserSurnameTextField().getText(),
                                view.getUserNAmeTextField().getText(), view.getUserPasswordTextField().getText().toCharArray(), User.ADMIN);
                        Utilization.addUser(st);
                        users.add(st);
                    }
                    view.getUserList().setModel(new ACIListModel(users));
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(view, ex);
            }
        }
    }

    private class AddSkillListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                if (view.getSkillTitleTextField().getText().isEmpty() || view.getSkillLevelTextField().getText().isEmpty()) {
                    throw new Exception("All fields must be filled!");
                } else {
                    Skill s = new Skill(view.getSkillTitleTextField().getText(), Integer.parseInt(view.getSkillLevelTextField().getText()));
                    if (!contains(s)) {
                        skills.add(s);
                        view.getSkillList().setModel(new ACIListModel(skills));
                        Utilization.addSkill(s.getTitle(), String.valueOf(s.getLevel()));
                    } else {
                        throw new Exception("Skill already exists!");
                    }
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(view, ex);
            }

        }

        private boolean contains(Skill s) {
            for (Iterator<Skill> it = skills.iterator(); it.hasNext();) {
                Skill skill = it.next();
                if (skill.getTitle().equals(s.getTitle())) {
                    return true;
                }
            }
            return false;
        }
    }

    private class DeleteSkillListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            Skill s = (Skill) view.getSkillList().getSelectedValue();
            skills.remove(s);
            view.getSkillList().setModel(new ACIListModel(skills));
        }
    }

    private class UpdateSkillListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            Skill s = (Skill) view.getSkillList().getSelectedValue();
            if (view.getSkillTitleTextField().getText().isEmpty() || view.getSkillLevelTextField().getText().isEmpty()) {
                JOptionPane.showMessageDialog(view, "All fields must be filled!");
            } else {
                s.setTitle(view.getSkillTitleTextField().getText());
                s.setLevel(Integer.parseInt(view.getSkillLevelTextField().getText()));
            }
        }
    }

    private class EditSkillsListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            changeForePanel(view.getSkillPanel());
        }
    }

    private class EditUsersListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            changeForePanel(view.getUserPanel());
        }
    }

    private class SkillSelectionListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            Skill s = (Skill) view.getSkillList().getSelectedValue();
            if (s != null) {
                view.getSkillTitleTextField().setText(s.getTitle());
                view.getSkillLevelTextField().setText(String.valueOf(s.getLevel()));
                view.getDeleteSkillButton().setEnabled(true);
                view.getUpdateSkillButton().setEnabled(true);
            } else {
                view.getDeleteSkillButton().setEnabled(false);
                view.getUpdateSkillButton().setEnabled(false);
                view.getSkillTitleTextField().setText("");
                view.getSkillLevelTextField().setText("");
            }
        }
    }

    private class UserSelectionListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            User u = (User) view.getUserList().getSelectedValue();
            if (u != null) {
                view.getUserNameTextField().setText(u.getName());
                view.getUserSurnameTextField().setText(u.getUsername());
                view.getUserNAmeTextField().setText(u.getUsername());
                view.getUserPasswordTextField().setText(String.valueOf(u.getPassword()));
                view.getUpdateUserButton().setEnabled(true);
                view.getDeleteUserButton().setEnabled(true);
                view.getUserRolesComboBox().setSelectedItem(u.getRole());
            } else {
                view.getUserNameTextField().setText("");
                view.getUserSurnameTextField().setText("");
                view.getUserNAmeTextField().setText("");
                view.getUserPasswordTextField().setText("");
                view.getUpdateUserButton().setEnabled(false);
                view.getDeleteUserButton().setEnabled(false);
                view.getUserRolesComboBox().setSelectedIndex(-1);
            }
        }
    }

    private class DeleteUserListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            User user = (User) view.getUserList().getSelectedValue();
            users.remove(user);
            view.getUserList().setModel(new ACIListModel(users));
        }
    }

    private class UpdateUserListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            User user = (User) view.getUserList().getSelectedValue();
            if (view.getUserNameTextField().getText().isEmpty() || view.getUserSurnameTextField().getText().isEmpty()
                    || view.getUserNAmeTextField().getText().isEmpty() || view.getUserPasswordTextField().getText().isEmpty()) {
                JOptionPane.showMessageDialog(view, "All of the fields must be filled!");
            } else {
                user.setName(view.getUserNameTextField().getText());
                user.setSurname(view.getUserSurnameTextField().getText());
                user.setUsername(view.getUserNAmeTextField().getText());
                user.setPassword(view.getUserPasswordTextField().getText().toCharArray());
            }
        }
    }
    private User admin = null;
    private AdminView view = null;
    private ArrayList<User> users = null;
    private ArrayList<Skill> skills = null;

    public AdminController(User admin, AdminView vw) {
        this.admin = admin;
        this.view = vw;
        this.users = Utilization.getUsers();
        this.skills = Utilization.getSkills();
        view.getEditUserButton().addActionListener(new EditUsersListener());
        view.getEditSkillsButton().addActionListener(new EditSkillsListener());
        view.getUpdateUserButton().addActionListener(new UpdateUserListener());
        view.getDeleteUserButton().addActionListener(new DeleteUserListener());
        view.getUserList().setModel(new ACIListModel(users));
        view.getUserList().addListSelectionListener(new UserSelectionListener());
        view.getSkillList().setModel(new ACIListModel(skills));
        view.getSkillList().addListSelectionListener(new SkillSelectionListener());
        view.getUpdateSkillButton().addActionListener(new UpdateSkillListener());
        view.getDeleteSkillButton().addActionListener(new DeleteSkillListener());
        view.getAddSkillButton().addActionListener(new AddSkillListener());
        view.getAddUserButton().addActionListener(new AddUserListener());

    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public User getAdmin() {
        return admin;
    }

    public AdminView getView() {
        return view;
    }

    public void changeForePanel(javax.swing.JPanel forePanel) {
        view.getUserList().clearSelection();
        view.getSkillList().clearSelection();
        view.getAdminLayeredPane().setLayer(view.getUserPanel(), 0);
        view.getUserPanel().setVisible(false);
        view.getAdminLayeredPane().setLayer(view.getSkillPanel(), 1);
        view.getSkillPanel().setVisible(false);
        view.getAdminLayeredPane().setLayer(forePanel, 2);
        forePanel.setVisible(true);


    }
}
